package com.erp.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.user.domain.City;
import com.erp.user.domain.Province;
import com.erp.user.domain.bo.ProvinceBo;
import com.erp.user.domain.vo.ProvinceVo;
import com.erp.user.mapper.CityMapper;
import com.erp.user.mapper.ProvinceMapper;
import com.erp.user.service.IProvinceService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 省份Service业务层处理
 *
 * @author erp
 * @date 2023-05-08
 */
@RequiredArgsConstructor
@Service
public class ProvinceServiceImpl implements IProvinceService {

    private final ProvinceMapper baseMapper;
    private final CityMapper cityMapper;
    /**
     * 查询省份
     */
    @Override
    public ProvinceVo queryById(Integer id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询省份列表
     */
    @Override
    public TableDataInfo<ProvinceVo> queryPageList(ProvinceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Province> lqw = buildQueryWrapper(bo);
        Page<ProvinceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        result.getRecords().forEach(vo -> {
            List<City> cities = cityMapper.selectList(new LambdaQueryWrapper<City>().eq(City::getProvinceId, vo.getId()));
            vo.setCities(cities);
        });
        return TableDataInfo.build(result);
    }

    /**
     * 查询省份列表
     */
    @Override
    public List<ProvinceVo> queryList(ProvinceBo bo) {
        LambdaQueryWrapper<Province> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Province> buildQueryWrapper(ProvinceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Province> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getProvince()), Province::getProvince, bo.getProvince());
        return lqw;
    }

    /**
     * 新增省份
     */
    @Override
    public Boolean insertByBo(ProvinceBo bo) {
        Province add = BeanUtil.toBean(bo, Province.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改省份
     */
    @Override
    public Boolean updateByBo(ProvinceBo bo) {
        Province update = BeanUtil.toBean(bo, Province.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Province entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除省份
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
